package com.zyl.pts.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.pts.config.enums.CodeAndMessageEnum;
import com.zyl.pts.domain.bean.FenceHouse;
import com.zyl.pts.domain.dto.FHDTO.FHUpdateOrAddDTO;
import com.zyl.pts.domain.vo.FHVO.FHByIdVO;
import com.zyl.pts.mapper.FHMapper;
import com.zyl.pts.service.FHService;
import com.zyl.pts.utils.CUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.service.serviceImpl
 * @ClassName: FHServiceImpl
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/19-15:31
 */
@Service
public class FHServiceImpl implements FHService {

    private final FHMapper fhMapper;

    public FHServiceImpl(FHMapper fhMapper) {
        this.fhMapper = fhMapper;
    }

    /**
     * 分页多条件查询栏舍信息
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param fhName 栏舍名称
     * @return {@link PageInfo }<{@link FenceHouse }>
     */
    @Override
    public PageInfo<FenceHouse> getFenceHouseList(int pageNum, int pageSize, String fhName) {
        Page<FenceHouse> page = PageHelper.startPage(pageNum,pageSize);
        List<FenceHouse> fhList = fhMapper.getFenceHouseList(fhName);
        PageInfo<FenceHouse> pageInfo = new PageInfo<>(fhList);
        page.close();
        return pageInfo;
    }

    /**
     * 根据栏舍编号查询栏舍信息以及栏圈信息
     * @param fhId 栏舍编号
     * @return {@link FHByIdVO }
     */
    @Override
    public FHByIdVO getFenceHouseById(String fhId) {
        return fhMapper.getFenceHouseAndHurdlesById(fhId);
    }

    /**
     * 新增或修改栏舍信息
     * @param fenceHouse 栏舍信息
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum addOrUpdateFenceHouse(FHUpdateOrAddDTO fenceHouse) {
        /*
            检查是否传入栏舍id
         */
        if(fenceHouse.getFhId() == null) {
            fenceHouse.setFhId(CUtil.UUID());
            return fhMapper.addFenceHouse(fenceHouse) == 0 ?
                    CodeAndMessageEnum.ERROR_INSERT : CodeAndMessageEnum.INSERT_SUCCESS;
        }
        return fhMapper.updateFenceHouse(fenceHouse) == 0 ?
                CodeAndMessageEnum.ERROR_UPDATE : CodeAndMessageEnum.UPDATE_SUCCESS;
    }

    /**
     * 根据id删除栏舍信息
     * @param fhId 栏舍编号
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteFenceHouseById(String fhId) {
        return fhMapper.deleteFenceHouse(fhId) == 0 ?
                CodeAndMessageEnum.ERROR_NOT_EXIST : CodeAndMessageEnum.DELETE_SUCCESS;
    }

    /**
     * 根据id列表删除栏舍信息
     * @param fhIdList 栏舍编号列表
     * @return {@link CodeAndMessageEnum }
     */
    @Override
    public CodeAndMessageEnum deleteFenceHouseByIdList(List<String> fhIdList) {
        return fhMapper.deleteFenceHouseBatch(fhIdList) ?
                CodeAndMessageEnum.DELETE_MANY_SUCCESS : CodeAndMessageEnum.ERROR_NOT_EXIST;
    }
}
